import scrapy

from scrapys.wudiSpyder.wudiSpyder.items import JingcaiCrsGameItem
from urllib.parse import urlencode

class Wan500JingcaiCrsSpider(scrapy.Spider):
    name = "Wan500JingcaiCrsSpider"
    allowed_domains = ["trade.500.com"]
    params = {'playid': 271,"g":2}
    query_string = urlencode(params)
    start_urls = [
        f"https://trade.500.com/jczq?{query_string}"  # 500wan 足球比分竞猜彩选购页面
        # f"https://trade.500.com/jczq/?playid=27"
    ]
    def parse(self, response):
        print("url:",response.url)
        tables = response.xpath('//*[@id="relativeContainer"]/div/div[3]/table')
        for table in tables:
            bet_trs = table.xpath("tbody/tr[@class='bet-tb-tr']")
            more_trs = table.xpath("tbody/tr[@class='bet-more-wrap']")
            for i in range(len(bet_trs)):
                tr = bet_trs[i]
                fid = tr.xpath("@data-fixtureid").get("data")  # 500wan比赛id
                expect = tr.xpath("@data-processdate").get("data")  # 竞猜比赛期数
                stop_time = tr.xpath("@data-buyendtime").get("data")  # 停止购买时间
                game_no_num = tr.xpath("@data-processname").get("data")  # 竞猜比赛场次
                game_no = tr.xpath("@data-matchnum").get("data")  # 竞猜比赛场次名称

                mtr = more_trs[i]
                f10 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='1:0']").get("data")
                f20 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='2:0']").get("data")
                f30 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='3:0']").get("data")
                f40 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='4:0']").get("data")
                f50 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='5:0']").get("data")
                f21 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='2:1']").get("data")
                f31 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='3:1']").get("data")
                f41 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='4:1']").get("data")
                f51 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='5:1']").get("data")
                f32 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='3:2']").get("data")
                f42 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='4:2']").get("data")
                f52 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='5:2']").get("data")
                fsother = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='胜其他']").get("data")
                s_bonus = "{},{},{},{},{},{},{},{},{},{},{},{},{}".format(f10,f20,f21,f30,f31,f32,f40,f41,f42,f50,f51,f52,fsother)

                f00 = mtr.xpath("td/div/table[2]/tbody/tr/td[1]/p[@data-value='0:0']").get("data")
                f11 = mtr.xpath("td/div/table[2]/tbody/tr/td[1]/p[@data-value='1:1']").get("data")
                f22 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='2:2']").get("data")
                f33 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='3:3']").get("data")
                fpother = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='平其他']").get("data")
                p_bonus = "{},{},{},{},{}".format(f00, f11, f22, f33,fpother)
                
                f01 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='0:1']").get("data")
                f02 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='0:2']").get("data")
                f03 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='0:3']").get("data")
                f04 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='0:4']").get("data")
                f05 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='0:5']").get("data")
                f12 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='1:2']").get("data")
                f13 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='1:3']").get("data")
                f14 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='1:4']").get("data")
                f15 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='1:5']").get("data")
                f23 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='2:3']").get("data")
                f24 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='2:4']").get("data")
                f25 = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='2:5']").get("data")
                ffother = mtr.xpath("td/div/table[1]/tbody/tr/td[1]/p[@data-value='负其他']").get("data")
                f_bonus = "{},{},{},{},{},{},{},{},{},{},{},{},{}".format(f01, f02, f12, f03, f13, f23, f04, f14, f24, f05,
                                                                     f15, f25, ffother)

                # 提取游戏信息
                game_item = JingcaiCrsGameItem()
                game_item['fid'] = fid
                game_item['expect'] = expect
                game_item['stop_time'] = stop_time
                game_item['game_no'] = game_no
                game_item['game_no_num'] = game_no_num
                game_item['crs_bonus'] = "{},{},{}".format(s_bonus,p_bonus,f_bonus)
                yield game_item



